﻿using DatabaseAccess;
using Liguo.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Liguo.Store
{
    public partial class ForgetPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void BtSendEmail_Click(object sender, EventArgs e)
        {
            SqlIt sql = new SqlIt();
            String UserName = TbUserName.Text;
            String MailBox = TbMailBox.Text;
            object id = sql.SingleSpecialItem("UserInfo", "UserName='" + UserName + "' and MailBox='" + MailBox + "'", "ID");
            if (id != null)
            {
                int UserID = Convert.ToInt32(id);
                SendEmail(UserName, MailBox, UserID);
            }
            else
            {
                Response.Write("<script>alert(\"无此用户！\")</script>");
                TbUserName.Text = "";
                TbMailBox.Text = "";
            }
        }

        private void SendEmail(String UserName, String MailBox, int UserID)
        {

            Dictionary<string, string> info = new Dictionary<string, string>();
            String XCode = Utilities.RandomString(64);
            info["XCode"] = XCode;
            info["XTime"] = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss");
            new SqlIt().UpdateMess("UserInfo", info, "ID='" + UserID + "'");
            new EMail().SendResetPwdMessage(UserName, MailBox, XCode);
            Response.Write("<script>alert('验证邮件发送成功，请尽查收！');</script>");
        }
    }
}